package com.bana.fvs.app.module.info.page;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.main.entity.UserInfoBean;
import com.bana.fvs.app.module.info.entity.LogOutBean;
import com.bana.fvs.app.module.login.page.LoginActivity;
import com.bana.fvs.app.mvp.contract.UserInfoContract;
import com.bana.fvs.app.mvp.presenter.UserInfoPresenter;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.app.AppManager;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.ToastUtil;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/2/8
 * Time   : 11:39
 * Desc   : 个人基本信息
 */
// TODO: 2018/3/11 登出相关没处理，接口不通
public class UserInfoActivity extends BaseActivity<UserInfoPresenter> implements UserInfoContract.IUserInfoView {

    @BindView(R.id.tvUserName)
    TextView mTvUserName;
    @BindView(R.id.tvConmpany)
    TextView mTvConmpany;
    @BindView(R.id.tvPhone)
    TextView mTvPhone;
    @BindView(R.id.tvLogOut)
    TextView mTvLogOut;
    @BindView(R.id.tvName)
    TextView mTvName;
//    @BindView(R.id.rlPhone)
//    FrameLayout rlPhone;

    @Override
    public int setLayout() {
        return R.layout.activity_information;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setLeftDrawable(-1);
        mBaseTitleBarView.setTitleText(getString(R.string.tip_user_info));
        mBaseTitleBarView.setLeftTitleGone();
    }

    @Override
    public void initWidget() {
//        rlPhone.setOnClickListener(this);
        mTvLogOut.setOnClickListener(this);
        mTvUserName.setText("");
        mTvPhone.setText("");
       // mPresenter.getUserInfo(this);
        setUserInfo();
    }

    private void setUserInfo() {
        UserInfoBean userInfo = UserInfoUtils.getInstance().getUserInfo(mContext);
        if (userInfo != null) {
            mTvUserName.setText(userInfo.getYhmc());
            mTvConmpany.setText(userInfo.getJgmc());
            mTvName.setText(userInfo.getXm());
        }
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tvLogOut:
               // http_logOut();
                UserInfoUtils.getInstance().clearCache(mContext);//清空缓存数据
                //退出登录今天登录页面
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                AppManager.getAppManager().finishOtherActivity(LoginActivity.class);
                finish();
                break;
//            case R.id.rlPhone:
//                String phone = mTvPhone.getText().toString();
//                Bundle bundle = new Bundle();
//                bundle.putString(ChangePhoneActivity.Params.PARAM_PHONE, phone);
//                startActivity(ChangePhoneActivity.class, bundle);
//                break;
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onTitleLeftPressed() {
        onBackPressed();
    }


    private void http_logOut() {
        mPresenter.http_logout(this);
    }

    @Override
    public void onError(CommException e) {

        ToastUtil.showShort(mContext, TextUtils.isEmpty(e.getMessage()) ? getString(R.string.get_user_info_error) : e.getMessage());
    }

    //登出回调
    @Override
    public void onResponse(LogOutBean outBean) {
        UserInfoUtils.getInstance().clearCache(mContext);//清空缓存数据
        //退出登录今天登录页面
        Intent intent = new Intent(mContext, LoginActivity.class);
        startActivity(intent);
        AppManager.getAppManager().finishOtherActivity(LoginActivity.class);
    }


    @Override
    public void onUpdatePhoneSuccess(String newPhone) {

    }

    @Override
    public void onUserInfoSuccess(UserInfoBean userInfoBean) {
        if (null != userInfoBean) {
            UserInfoUtils.getInstance().saveUserInfo(this, userInfoBean);
            fillValues(userInfoBean);
        }
    }

    /**
     * 赋值
     */
    private void fillValues(UserInfoBean bean) {
        mTvUserName.setText(bean.getYhmc());
        mTvConmpany.setText(bean.getJgmc());
        mTvName.setText(bean.getXm());
    }
}
